#!/bin/bash

##########################
#日志分割


#不能重复执行,不然会替换掉原来的文件
##########################




nginx_conf_path='/etc/nginx/sites-enabled/'

########### fun ############






#----get log list

#按照配置移动文件，
move_log(){
    loop=''
    for loop in $log_line
    do
        log_new_name=$(sed -n  's/^[\t ]*\([^\t ]*\)\.log.*$/\1_/ipg' <(echo "$loop"))
        mv  "$loop"  "$log_new_name`date +%Y%m%d`.log"
        echo "$loop"  "$log_new_name`date +%Y%m%d`.log"

    done
    kill -s USR1 `cat /var/run/nginx.pid`
}

#所有日志配置行
get_log_line(){
    
   conf_list=$(ls "$nginx_conf_path"*.conf)
   for loop in $conf_list
    do
        #access_log
        #log_line_tmp=$(sed -n  's/^[\t\s]*access_log\(.*\)[\t\s]*;.*$/ \1 /pg' "$loop")
        log_line_tmp=$(sed -n  's/^[\t ]*access_log[\t ]*\([^\t ]*\)[\t ]*.*;.*$/ \1 /ipg' "$loop")

        log_line=$log_line$log_line_tmp
    done  
}
################start################
get_log_line
move_log
